<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property string $id
 * @property string $unique_id
 * @property string $username
 * @property string $c_username
 * @property string $firstname
 * @property string $lastname
 * @property string $birthday
 * @property string $email
 * @property string $mobile
 * @property string $phone
 * @property string $manager
 * @property string $department
 * @property string $code
 * @property integer $project_group
 * @property string $attribute
 * @property integer $company
 * @property string $position
 * @property string $position_en
 * @property string $entry_point
 * @property integer $work_type
 * @property integer $status
 * @property integer $dateline
 */
class HecUser extends CActiveRecord
{

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return HecUser the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('position_en', 'required'),
			//array('project_group, company, work_type, status, dateline', 'numerical', 'integerOnly'=>true),
			//array('unique_id, mobile, phone', 'length', 'max'=>20),
			//array('username, c_username, firstname, lastname, email, manager, department, code, attribute, position, position_en, entry_point', 'length', 'max'=>64),
			//array('birthday', 'length', 'max'=>32),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			//array('id, unique_id, username, c_username, firstname, lastname, birthday, email, mobile, phone, manager, department, code, project_group, attribute, company, position, position_en, entry_point, work_type, status, dateline', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'unique_id' => 'Unique',
			'username' => 'Username',
			'c_username' => 'C Username',
			'firstname' => 'Firstname',
			'lastname' => 'Lastname',
			'birthday' => 'Birthday',
			'email' => 'Email',
			'mobile' => 'Mobile',
			'phone' => 'Phone',
			'manager' => 'Manager',
			'department' => 'Department',
			'bu' => 'BU',
			'code' => 'Code',
			'project_group' => 'Project Group',
			'attribute' => 'Attribute',
			'company' => 'Company',
			'position' => 'Position',
			'position_en' => 'Position En',
			'entry_point' => 'Entry Point',
			'work_type' => 'Work Type',
			'gender' => 'Gender',
			'probation' => 'probation',
			'status' => 'Status',
			'dateline' => 'Dateline',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		//$criteria->compare('id',$this->id);
		//$criteria->compare('unique_id',$this->unique_id);
		$criteria->compare('username',$this->username,true);
		//$criteria->compare('c_username',$this->c_username,true);
		//$criteria->compare('firstname',$this->firstname,true);
		/*
		$criteria->compare('lastname',$this->lastname,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('manager',$this->manager,true);
		$criteria->compare('department',$this->department,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('project_group',$this->project_group);
		$criteria->compare('attribute',$this->attribute,true);
		$criteria->compare('company',$this->company);
		$criteria->compare('position',$this->position,true);
		$criteria->compare('position_en',$this->position_en,true);
		$criteria->compare('entry_point',$this->entry_point,true);
		$criteria->compare('work_type',$this->work_type);
		$criteria->compare('status',$this->status);
		$criteria->compare('dateline',$this->dateline);
		*/

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
			'pagination'=>array(  
                'pageSize'=>20,
            ),

		));
	}

	public function getUserInfoByName($username)
	{
		return $this->findByAttributes(array('username'=>$username)); 
	}


	/**
	 * 身份认证
	 * 获取用户用户组
	 *
	 */
	public function identityGroup($uniqueId,$username)
	{
		$group['admincp'] = 0;
		$group['transfer'] = 1; //普通元素
		//是不是后台管理员
		$adminUser = new HrAdminUser;
		$group_id = $adminUser->checkAdmin($username);
		$group['admincp'] = $group_id>0?$group_id:0; //返回group id
		$UserDepartmentModel = new UserDepartment;
		$departmentManager = $UserDepartmentModel->checkManager($uniqueId);
		//是不是部门主管
		if($departmentManager)
		{
			$group['transfer'] = 2;
		}
		//是不是部门助理
		if($UserDepartmentModel->checkAdminAssistant($uniqueId))
		{
			$group['transfer'] = 3;
		}
		//是不是hr的总监或管理员
		$hrTransferManager = HrOption::model()->findAllByAttributes(array(),'option_name LIKE "transfer_hr%"');
		if($hrTransferManager)
		{
			foreach ($hrTransferManager as $key => $value) 
			{
				if($uniqueId == $value->option_value)
				{
					$group['transfer'] = 4;
					break;
				}
			}
		}
		//transfer 管理员
		$transferAdmin = HrOption::model()->findAllByAttributes(array(),'option_name LIKE "transfer_admin%"');
		if($transferAdmin)
		{
			foreach ($transferAdmin as $key => $value) 
			{
				if($uniqueId == $value->option_value)
				{
					$group['transfer'] = 5;
					break;
				}
			}
		}
		return $group;
	}


	public function getAttributeName($id)
	{
		$arr = Tinit::attributeName();
		return $arr[$id];
	}

	public function getAttribute()
	{
		return Tinit::attributeName();
		//return $arr[$id];
	}

	public function getWorkType()
	{
		return Tinit::workTypeName();
	}

	public function getWorkTypeName($id)
	{
		$arr = Tinit::workTypeName();
		return $arr[$id];
	}

	public function getGender()
	{
		return Tinit::genderName();
	}

	public function getGenderName($id)
	{
		$arr = Tinit::genderName();
		return $arr[$id];
	}

	
	public function getCompanyName($id)
	{
		$arr = Tinit::companyName();
		return $arr[$id];
	}

	public function getCompany()
	{
		return Tinit::companyName();
	}

	public function getInfoByUniqueid($uniqueId,$column)
	{
		$temp = HecUser::model()->findByAttributes(array('unique_id'=>$uniqueId));
		return $temp->$column?$temp->$column:null;
	}

	//判断一组调动人中，是否有美术
	public function checkArt($array)
	{
		$artFlag = 0;
		if(!empty($array))
		{
			foreach ($array as $value) {
				if($value)
				{
					$attribute = HecUser::model()->getInfoByUniqueid($value,'attribute');
					if($attribute == 2)
					{
						$artFlag = 1;
						break;
					}
				}
			}
		}
		return $artFlag;
	}

}